package com.pengtu.gsj.service.impl.zxgc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.zxgc.SpecialProjectDao;
import com.pengtu.gsj.entity.app.Organization;
import com.pengtu.gsj.entity.base.LabelValue;
import com.pengtu.gsj.entity.zxgc.ProjectData;
import com.pengtu.gsj.entity.zxgc.SpecialProject;
import com.pengtu.gsj.service.system.OrganizationService;
import com.pengtu.gsj.service.zxgc.ProjectDataService;
import com.pengtu.gsj.service.zxgc.SpecialProjectService;
import com.pengtu.gsj.utils.ExcelImportUtils;
import com.pengtu.gsj.utils.LabelHelper;
import com.pengtu.gsj.utils.MathsUtils;

@Service
@Transactional
public class SpecialProjectServiceImpl implements SpecialProjectService {
	
	@Autowired
	private SpecialProjectDao specialProjectDao;
	@Autowired
	private ProjectDataService projectDataService;
	@Autowired
	private OrganizationService organizationService;

	@Override
	public void save(SpecialProject specialProject) {
		specialProjectDao.save(specialProject);
	}

	@Override
	public SpecialProject get(String id) {
		return specialProjectDao.get(id);
	}

	@Override
	public Page<SpecialProject> getPage(Page<SpecialProject> page,
			QueryFilter queryFilter) {
		return specialProjectDao.findPage(page, queryFilter);
	}

	@Override
	public List<SpecialProject> getSpecialProjects(QueryFilter queryFilter) {
		return specialProjectDao.find(queryFilter);
	}

	@Override
	public void delete(String id) {
		specialProjectDao.delete(id);
	}

	@Override
	public void delete(SpecialProject specialProject) {
		specialProjectDao.delete(specialProject);
	}

	@Override
	public Map<String, Object> getStaticData(String projectId,
			Map<String, Object> map) {
		//保存统计的每一行数据
		List<List<String>> lineList = new ArrayList<List<String>>();
		//保存具体的专项数据
		List<String> datas;
		//保存专项数据名称
		List<String> dataTitles = new ArrayList<String>();
		
		if (StringUtils.isEmpty(projectId)) {
			return map;
		} else {
			SpecialProject specialProject = get(projectId);
			if (specialProject == null) {
				return map;
			} else {
				dataTitles = Arrays.asList(specialProject.getDataTab().split(";"));
				for (ProjectData projectData : specialProject.getProjectDatas()) {
					datas = new ArrayList<String>();
					Organization organization = organizationService.get(projectData.getOrgId());
					if (organization != null) {
						datas.add(organization.getOrgName());
					} else {
						datas.add("缺失");
					}
					datas.addAll(Arrays.asList(projectData.getDataContent().split(";")));
					lineList.add(datas);
				}
				map.put("dataTitles", dataTitles);
				map.put("lineList", lineList);
				return map;
			}
		}
		
	}

	
	/**
	 * 
	 * getStatisticsSpecialProjects:
	 * 适用:统计某年度下所有符合要求（非作废）的专项工程数量
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public List<SpecialProject> getStatisticsSpecialProjects() {
		return specialProjectDao.getStatisticsSpecialProjects();
	}

	/**
	 * 
	 * getSpecialProjectsByStatus:
	 * 适用:获得某年度下非作废状态项目的分类数据
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	public List<SpecialProject> getSpecialProjectsByStatus() {
		return specialProjectDao.getSpecialProjectsByStatus();
	}

	@Override
	public List<Map<String, Object>> getCountJson() {
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		Map<String, Object> map = null;
		List<SpecialProject> allSpecialProjects = getStatisticsSpecialProjects();
		List<SpecialProject> statusSpecialProjects = getSpecialProjectsByStatus();
		List<LabelValue> labelValues = LabelHelper.getInstance().buildCodeNames(6009);
		long completeCount = 0;
		for (SpecialProject specialProject : allSpecialProjects) {
			map = new HashMap<String, Object>();
			map.put("name", specialProject.getYear());
			map.put("group", "总数");
			map.put("value", specialProject.getCount());
			list.add(map);
			for (LabelValue labelValue : labelValues) {
				if ("1".equals(labelValue.getValue())) {
					continue;
				} else {
					map = new HashMap<String, Object>();
					map.put("name", specialProject.getYear());
					map.put("group", labelValue.getLabel());
					map.put("value", 0);
					Iterator<SpecialProject> iterator = statusSpecialProjects.iterator();
					while (iterator.hasNext()) {
						SpecialProject statusSpecialProject = (SpecialProject) iterator.next();
						if (statusSpecialProject.getStatus().equals(labelValue.getValue()) && specialProject.getYear().equals(statusSpecialProject.getYear())) {
							if ("2".equals(statusSpecialProject.getStatus())) {
								completeCount = statusSpecialProject.getCount(); 
							}
							map.put("value", statusSpecialProject.getCount());
							iterator.remove();
							break;
						}
					}
					list.add(map);
				}
			}
			map = new HashMap<String, Object>();
			map.put("name", specialProject.getYear());
			map.put("group", "完成率（%）");
			map.put("value", MathsUtils.divDouble(completeCount*100, specialProject.getCount(), 2));
			list.add(map);
		}
		return list;
	}

	@Override
	public ResponseEntity<byte[]> exportData() throws Exception {
		List<SpecialProject> allSpecialProjects = getStatisticsSpecialProjects();
		List<SpecialProject> statusSpecialProjects = getSpecialProjectsByStatus();
		List<LabelValue> labelValues = LabelHelper.getInstance().buildCodeNames(6009);
		long completeCount = 0;
		List<List<String>> list = new ArrayList<List<String>>();
		for (SpecialProject specialProject : allSpecialProjects) {
			List<String> data = new ArrayList<String>();
			data.add(specialProject.getYear());
			data.add(String.valueOf(specialProject.getCount()));
			for (LabelValue labelValue : labelValues) {
				long count = 0;
				if ("1".equals(labelValue.getValue())) {
					continue;
				} else {
					Iterator<SpecialProject> iterator = statusSpecialProjects.iterator();
					while (iterator.hasNext()) {
						SpecialProject statusSpecialProject = (SpecialProject) iterator.next();
						if (statusSpecialProject.getStatus().equals(labelValue.getValue()) && specialProject.getYear().equals(statusSpecialProject.getYear())) {
							if ("2".equals(statusSpecialProject.getStatus())) {
								completeCount = statusSpecialProject.getCount(); 
							}
							count = statusSpecialProject.getCount();
							iterator.remove();
							break;
						}
					}
					data.add(String.valueOf(count));
				}
			}
			data.add(String.valueOf(MathsUtils.divDouble(completeCount*100, specialProject.getCount(), 2)));
			list.add(data);
		}
		return ExcelImportUtils.insertDataToExcel("specialProject.xls", list, "专项工程汇总表.xls");
	}

	@Override
	public List<SpecialProject> getDealProjectsByStatus() {
		return specialProjectDao.findBy("status", "0");
	}


}
